package com.efei.jdbc.sql;

import org.springframework.util.StringUtils;

import java.util.List;

public class SqlDelete extends SqlCommon{

    private final StringBuilder where = new StringBuilder();
    //private List<SqlCondition> conditions = null;
    private String table;


    SqlDelete() {
        super(SqlType.DELETE);
    }

    public static SqlDelete of() {
        return new SqlDelete();
    }

    public SqlDelete table(String table) {
        this.table = table;
        return this;
    }


    public SqlDelete where(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        //this.where.append(" ");
        this.where.append(tb);
        return this;
    }

    public SqlDelete whereAnd(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        if(where.length()>0) {
            this.where.append(" and ");
        }
        this.where.append(tb);
        return this;
    }

    public SqlDelete whereAnd(SqlCondition c) {
        if(where.length()>0) {
            this.where.append(" and ");
        }
        //conditions.add(c);
        this.where.append(c.getColumn());
        this.where.append(c.getOperator());
        String alis = c.getColumn().replace(".", "_");
        this.where.append(":"+alis);
        this.addParameter(alis, c.getValue());
        return this;
    }

    public SqlDelete whereOr(String tb) {
        if(StringUtils.hasLength(tb)) {
            return this;
        }
        if(where.length()>0) {
            this.where.append("or");
        }
        this.where.append(tb);
        return this;
    }

    public SqlDelete whereOr(SqlCondition c) {
        if(where.length()>0) {
            this.where.append("or");
        }
        //conditions.add(c);
        this.where.append(c.getColumn());
        this.where.append(c.getOperator());
        String alis = c.getColumn().replace(".", "_");
        this.where.append(":"+alis);
        this.addParameter(alis, c.getValue());
        return this;
    }

    @Override
    public StringBuilder getSql() {
        // TODO Auto-generated method stub
        StringBuilder sb = new StringBuilder();
        sb.append("delete from ").append(table).append(" where ");
        sb.append(this.where);
        return sb;
    }
}
